package cn.yuanqiao.web.controller.archive.collection.danganjieshou;

import cn.yuanqiao.archive.collection.danganjieshou.domain.YqGcReceiveEfile;
import cn.yuanqiao.archive.collection.danganjieshou.domain.YqGcReceiveFile;
import cn.yuanqiao.archive.collection.danganjieshou.domain.YqGcReceiveFolder;
import cn.yuanqiao.archive.collection.danganjieshou.service.impl.YqGcReceiveServiceImpl;
import cn.yuanqiao.common.core.domain.R;
import cn.yuanqiao.common.web.controller.BaseController;
import cn.yuanqiao.common.core.domain.AjaxResult;
import cn.yuanqiao.common.web.page.TableDataInfo;
import cn.yuanqiao.common.annotation.Log;
import cn.yuanqiao.common.enums.BusinessType;
import cn.yuanqiao.common.core.context.ResponseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.List;
import java.util.Objects;

/**
 * 馆藏文件接收
 *
 * @ProjectName: JZSDAS
 * @Package: cn.yuanqiao.archive.collection.danganjieshou.controller
 * @ClassName: YqGcReceiveController
 * @Author: lenovo
 * @Description: hbWang
 * @Date: 2023/8/3 14:19
 * @Version: 1.0
 */
@RestController
@RequestMapping("/archive/collection/receive")
public class YqGcReceiveController extends BaseController {

    @Autowired
    private YqGcReceiveServiceImpl yqGcReceiveService;

    /**
     * 查询组卷列表
     *
     * @param yqGcReceiveFolder
     * @return
     */
    @GetMapping("/folder/list")
    public TableDataInfo listFolder(YqGcReceiveFolder yqGcReceiveFolder) {
        startPage();
        List<YqGcReceiveFolder> list = yqGcReceiveService.selectFolder(yqGcReceiveFolder);
        return getDataTable(list);
    }


    /**
     * 查询组件列表
     *
     * @param yqGcReceiveFile
     * @return
     */
    @GetMapping("/file/list")
    public TableDataInfo listFile(YqGcReceiveFile yqGcReceiveFile) {
        startPage();
        List<YqGcReceiveFile> list = yqGcReceiveService.selectFile(yqGcReceiveFile);
        return getDataTable(list);
    }

    /**
     * 查询组卷详情
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/folder/{id}")
    public AjaxResult getInfoFolder(@PathVariable("id") String id) {
        return success(yqGcReceiveService.selectFolderId(id));
    }

    /**
     * 查询组件详情
     *
     * @param id
     * @return
     */
    @GetMapping(value = "/file/{id}")
    public AjaxResult getInfoFile(@PathVariable("id") String id) {
        return success(yqGcReceiveService.selectFileId(id));
    }

    /**
     * 组卷著录
     *
     * @param yqGcReceiveFolder
     * @return
     */
    @Log(title = "馆藏文件接收-组卷著录", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add/folder")
    public AjaxResult addFolder(@RequestBody YqGcReceiveFolder yqGcReceiveFolder) {
        return success(yqGcReceiveService.insertFolder(yqGcReceiveFolder));
    }

    /**
     * 组件著录
     *
     * @param yqGcReceiveFile
     * @return
     */
    @Log(title = "馆藏文件接收-组件著录", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add/file")
    public AjaxResult addFile(@RequestBody YqGcReceiveFile yqGcReceiveFile) {
        return toAjax(yqGcReceiveService.insertFile(yqGcReceiveFile));
    }

    /**
     * 组卷修改
     *
     * @param yqGcReceiveFolder
     * @return
     */
    @Log(title = "馆藏文件接收-组卷修改", businessType = BusinessType.UPDATE)
    @PutMapping(value = "/edit/folder")
    public AjaxResult editFolder(@RequestBody YqGcReceiveFolder yqGcReceiveFolder) {
        return toAjax(yqGcReceiveService.updateFolder(yqGcReceiveFolder));
    }

    /**
     * 组件修改
     *
     * @param yqGcReceiveFile
     * @return
     */
    @Log(title = "馆藏文件接收-组件修改", businessType = BusinessType.UPDATE)
    @PutMapping(value = "/edit/file")
    public AjaxResult editFile(@RequestBody YqGcReceiveFile yqGcReceiveFile) {
        return toAjax(yqGcReceiveService.updateFile(yqGcReceiveFile));
    }

    /**
     * 组卷删除
     *
     * @param ids
     * @return
     */
    @Log(title = "馆藏文件接收-组卷删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/folder/{ids}")
    public AjaxResult removeFolder(@PathVariable String[] ids) {
        return toAjax(yqGcReceiveService.delFolder(ids));
    }

    /**
     * 组件删除
     *
     * @param ids
     * @return
     */
    @Log(title = "馆藏文件接收-组件删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/file/{ids}")
    public AjaxResult removeFile(@PathVariable String[] ids) {
        return toAjax(yqGcReceiveService.delFile(ids));
    }

    /**
     * 文件上传请求
     */
    @PostMapping("/upload")
    R<YqGcReceiveEfile> uploadPicture(MultipartFile file) {
        return yqGcReceiveService.uploadFile(file);
    }


    /**
     * 文件预览
     *
     * @param response
     * @throws Exception
     */
    @PostMapping("/preview")
    public void view(HttpServletResponse response, @RequestParam("path") String path)
            throws Exception {
        byte[] bytes = yqGcReceiveService.queryImage(path);
        // 以流的形式返回
        ServletOutputStream out = null;
        ByteArrayOutputStream baos = null;
        try {
            InputStream inStream = new ByteArrayInputStream(bytes);
            byte[] buffer = new byte[1024];
            int len;
            baos = new ByteArrayOutputStream();
            while ((len = inStream.read(buffer)) != -1) {
                baos.write(buffer, 0, len);
            }
            response.reset(); // 非常重要
            // 设置 CORS 响应头
            ResponseUtils.defaultResponse(response);
            //在线预览，浏览器支持的文件类型都能打开
            response.setContentType("application/octet-stream");
            out = response.getOutputStream();
            out.write(baos.toByteArray());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            Objects.requireNonNull(baos).flush();
            baos.close();
            Objects.requireNonNull(out).flush();
            out.close();
        }
    }

}
